<?php
namespace app\admin\controller;

use app\BaseController;
use think\Request;

class Video extends BaseController
{
    public function list()
    {
        try {
            return json([
                'code' => 20000,
                'message' => 'OK',
                'data' => [
                    'items' => [
                        [
                            'id' => 1,
                            'title' => '视频01',
                            'description' => '视频01 description'
                        ],
                        [
                            'id' => 2,
                            'title' => '视频02',
                            'description' => '视频02 description'
                        ],
                        [
                            'id' => 3,
                            'title' => '视频03',
                            'description' => '视频03 description'
                        ],
                    ],
                    'total' => 50
                ]
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 4000,
                'message' => '退出失败'
            ]);
        }
    }

    public function create(Request $request)
    {
        try {
            $username = $request->param('username', '');
            $password = $request->param('password', '');

            if ($username == 'admin' && $password == '111111') {
                return json([
                    'code' => 20000,
                    'message' => 'OK',
                    'data' => [
                        'token' => 'admin-token'
                    ]
                ]);
            }

            return json([
                'code' => 4001,
                'message' => '用户名或密码不正确'
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 4000,
                'message' => '用户名或密码不正确'
            ]);
        }
    }

    public function update()
    {
        try {
            return json([
                'code' => 20000,
                'message' => 'OK',
                'data' => [
                    'roles' => ['admin'],
                    'introduction' => 'I am a super administrator',
                    'avatar' => 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                    'name' => 'Super Admin'
                ]
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 4000,
                'message' => '获取用户信息失败'
            ]);
        }
    }

    public function detail()
    {
        try {
            return json([
                'code' => 20000,
                'message' => 'OK',
                'data' => 'success'
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 4000,
                'message' => '退出失败'
            ]);
        }
    }

    public function cover(Request $request)
    {
        try {
            var_dump($request->file('file'));

            return json([
                'code' => 20000,
                'message' => 'cover',
                'data' => ['qiniu_url' => 'https://img1.sycdn.imooc.com/58cb92df0001493305580343.jpg']
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 4000,
                'message' => 'cover'
            ]);
        }
    }

    public function upload(Request $request)
    {
        try {
            $videoFile = $request->file('video_file');
            $coverFile = $request->file('cover_file');

            $videoFileSavename = '';
            $coverFileSavename = '';
            if (!is_null($videoFile)) {
                $videoFileSavename = \think\facade\Filesystem::putFile('video', $videoFile);
            }
            if (!is_null($coverFile)) {
                $coverFileSavename = \think\facade\Filesystem::putFile('cover', $coverFile);
            }

            //

            return json([
                'code' => 20000,
                'message' => 'upload',
                'data' => ['qiniu_url' => 'https://img1.sycdn.imooc.com/58cb92df0001493305580343.jpg'],
                'request' => [
                    'title' => $request->param('title'),
                    'description' => $request->param('description'),
                    'videoFileSavename' => $videoFileSavename,
                    'coverFileSavename' => $coverFileSavename
                    // 'coverFile' => $coverFile == null
                    // 'description' => $request->param('description')
                ]
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 4000,
                'message' => 'upload'
            ]);
        }
    }
}
